import { Body, Controller, Delete, Get, Param, Post, Put, UseGuards } from "@nestjs/common";
import { ApiOperation, ApiTags } from "@nestjs/swagger";
import { JwtAuthGuard } from "../../auth/guard/jwt-auth.guard";
import { RequireRoles } from "../decorator/require-role.decorator";
import { RoleAddAccessDto } from "../dto/role.dto";
import { Role } from "../entities/role.entity";
import { RoleGuard } from "../guards/role.guard";
import { RoleService } from "../service/role.service";

@UseGuards(JwtAuthGuard, RoleGuard)
@ApiTags('role - 角色')
@Controller('role')
export class RoleController {
    constructor(
        private readonly roleService: RoleService
    ) { }

    @Post()
    @ApiOperation({
        summary: "新增用户身份",
    })
    async create(@Body() role: Role) {
        return await this.roleService.create(role);
    }

    @Put()
    @ApiOperation({
        summary: "修改用户身份",
    })
    async update(@Body() role: Role) {
        return await this.roleService.update(role);
    }

    @Put("/updateRoleAccess")
    @ApiOperation({
        summary: "修改某个用户身份所具有的权限",
    })
    async updateRoleAccess(@Body() roleAddAccessDto: RoleAddAccessDto) {
        return await this.roleService.updateRoleAccess(roleAddAccessDto);
    }

    @Get()
    @RequireRoles("admin")
    @ApiOperation({
        summary: "查询所有身份所具有的权限",
    })
    async findAll() {
        // return await Role.find()
        return await this.roleService.findAll();
    }

    @Delete("/:id")
    @ApiOperation({
        summary: "删除某个身份",
    })
    async delete(@Param("id") id: number) {
        return await this.roleService.remove(id);
    }

}